package com.gph.etrip;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.gph.etrip.timmy.test.AndroidSaxXMLMarkerParser;
import com.gph.etrip.timmy.test.Marker;
import com.gph.etrip.timmy.test.ViewAdapter;

	public class ActivityList extends Activity {


	    ListView list;
	    ViewAdapter adapter;
//	    String[] mStrings;
	
	    ArrayList<Marker> markerList = new ArrayList<Marker>();
	    private String selectedMarker = null;
	    private String markerUrl= null;
	    private String category= null;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
			Intent intent = getIntent();
			category = intent.getStringExtra("cate2route");
	        
	        try {
	        	
				markerList = getMarkerList();

				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        list=(ListView)findViewById(R.id.list);
	        adapter=new ViewAdapter(this,markerList);
	        list.setAdapter(adapter);

	        
	        final DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
	        	  
	    		  public void onClick(DialogInterface dialog, int which) {
	    		        switch (which){
	    		        case DialogInterface.BUTTON_POSITIVE:
	    		        	
	    		        	//Toast.makeText(getApplicationContext(), "11111", Toast.LENGTH_LONG).show();
	    		        	
//	    		    		Intent intent= new Intent();
//	    					intent.putExtra("testIntent", selectedMarker);
//	    					intent.setClass(RewardsListView.this,SingleMarkerView.class);
//	    					RewardsListView.this.startActivity(intent);
	    		            break;
	    		            
	    		        case DialogInterface.BUTTON_NEGATIVE:
	    		            //No button clicked
	    		            break;
	    		        }
	    		    }
	    		};
	    		list.setOnItemClickListener(new OnItemClickListener() {

					public void onItemClick(AdapterView<?> parent, View arg1,
							int position, long arg3) {
						selectedMarker = (String) adapter.getMarkerName(position);
						markerUrl = (String) adapter.getMarkerIcon(position);
//						int m//kerId =((Marker)adapter.getItem(posit//n)).getId();
						Intent intent= new Intent();
						intent.putExtra("testIntent", selectedMarker);
    					intent.putExtra("testIntent1", markerUrl);
    					intent.setClass(ActivityList.this,ActivityLayout.class);
    					ActivityList.this.startActivity(intent);
//						
						/*
			    		AlertDialog.Builder builder = new AlertDialog.Builder(parent.getContext());
			    		builder.setMessage("Marker Type?").setPositiveButton("Current Location", dialogClickListener)
			    		    .setNegativeButton("With Distance", dialogClickListener).show();	
		    		    */
					}
	        	  });
	    }
	    
	    @Override
	    public void onDestroy()
	    {
	        adapter.imageLoader.stopThread();
	        list.setAdapter(null);
	        super.onDestroy();
	    }
	    
	    public OnClickListener listener=new OnClickListener(){
	        public void onClick(View arg0) {
	            adapter.imageLoader.clearCache();
	            adapter.notifyDataSetChanged();
	        }
	    };
	    
	    /**
	     * Retrieve defined markers provided in markerconfig.xml, in the assets folder.
	     * @return ArrayList of markers found in the configuration file.
	     */
	    private ArrayList<Marker> getMarkerList() throws FileNotFoundException {
	    	AndroidSaxXMLMarkerParser markerParser;
			try {
				markerParser = new AndroidSaxXMLMarkerParser();
				return markerParser.parse(getAssets().open("activitylist.xml"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	return null; 

		}
	  
}
